#include <algorithm>
#include <cstdint>
#include <iostream>
#include <istream>
#include <limits>
#include <vector>

using ll = int64_t;

ll n,m;
std::vector<ll> a,b;
ll ans=std::numeric_limits<ll>::max();

int main(){
    std::iostream::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    std::cin>>n>>m;
    a.resize(n+1);
    b.resize(n+1);

    for(ll i=1;i<=n;i++){
        std::cin>>a[i];
    }
    for(ll i=1;i<=n;i++){
        std::cin>>b[i];
    }
    std::sort(a.begin()+1,a.end());
    std::sort(b.begin()+1,b.end());
    while (std::next_permutation(a.begin()+1,a.end())) {
        while(std::next_permutation(b.begin()+1,b.end())){
            ll nans = std::numeric_limits<ll>::min();
            for(ll i=1;i<=n;i++){
                nans=std::max(nans,(a[i]+b[i])%m);
            }
            ans=std::min(nans,ans);
        }
    }
    std::cout<<ans<<'\n';
}